import axios from 'axios'

//设置baseurl
const baseUrl = 'http://develop-api.deepcode.studio'

export const post = (url: string, data = {}, token = '') => {
  if (token) {
    return new Promise((resolve, reject) => {
      axios
        .post(baseUrl + url, data, {
          headers: { 'Content-Type': 'application/json', Authorization: token }
        })
        .then(
          (response) => {
            resolve(response.data)
          },
          (err) => {
            reject(err)
          }
        )
    })
  } else {
    return new Promise((resolve, reject) => {
      axios
        .post(baseUrl + url, data, {
          headers: { 'Content-Type': 'application/json' }
        })
        .then(
          (response) => {
            console.log('请求成功', response.data)
            resolve(response.data)
          },
          (err) => {
            console.log('请求失败', err)
            reject(err)
          }
        )
    })
  }
}

export const get = (url: string, token: string) => {
  return new Promise((resolve, reject) => {
    axios
      .get(baseUrl + url, {
        headers: { Authorization: token }
      })
      .then(
        (response) => {
          resolve(response.data)
          console.log(response.data)
        },
        (err) => {
          console.log(err)
          reject(err)
        }
      )
  })
}

export const put = (url: string, data = {}, token: string) => {
  return new Promise((resolve, reject) => {
    axios
      .put(baseUrl + url, data, {
        headers: { 'Content-Type': 'application/json', Authorization: token }
      })
      .then(
        (response) => {
          resolve(response.data)
        },
        (err) => {
          reject(err)
        }
      )
  })
}

export const detele = (url: string, data = {}, token: string) => {
  return new Promise((resolve, reject) => {
    axios
      .delete(baseUrl + url, {
        headers: { Authorization: token },
        data: data
      })
      .then(
        (response) => {
          resolve(response.data)
        },
        (err) => {
          reject(err)
        }
      )
  })
}

export const upload = (url: string, data = {}, token: string) => {
  return new Promise((resolve, reject) => {
    axios
      .post(baseUrl + url, data, {
        headers: {
          'Content-Type': 'multipart/form-data',
          Authorization: token,
          //@ts-ignore
          transformRequest: [(data) => data]
        }
      })
      .then(
        (response) => {
          // console.log('请求成功', response.data)
          resolve(response.data)
        },
        (err) => {
          // console.log('请求失败', err)
          reject(err)
        }
      )
  })
}
export const getStream = (url: string) => {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        responseType: 'arraybuffer'
      })
      .then(
        (response) => {
          // console.log('请求成功', response.data)
          resolve(response.data)
        },
        (err) => {
          // console.log('请求失败', err)
          reject(err)
        }
      )
  })
}
